package com.woniuxy.courseservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.status.OrderState;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.courseclient.client.OrderFeignClient;
import com.woniuxy.courseclient.client.ProductFeignClient;
import com.woniuxy.courseclient.client.UserFeignClient;
import com.woniuxy.courseclient.client.VenueFeignClient;
import com.woniuxy.courseclient.client.dto.OrderDto;
import com.woniuxy.courseclient.client.dto.TeamProductDto;
import com.woniuxy.courseclient.client.dto.UserInfoDto;
import com.woniuxy.courseclient.client.dto.VenueLoginDto;
import com.woniuxy.coursedao.mapper.TeamCourseMapper;
import com.woniuxy.coursedao.mapper.TeamSignInMapper;
import com.woniuxy.coursedao.model.TeamCourse;
import com.woniuxy.coursedao.model.TeamSignIn;
import com.woniuxy.coursedao.model.TeamVacate;
import com.woniuxy.coursemodel.dto.TeamSignInDto;
import com.woniuxy.courseservice.service.TeamSignInService;
import com.woniuxy.courseservice.service.exception.OrdinaryCourseException;
import com.woniuxy.courseservice.service.exception.TeamSignInException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 团课签到记录表 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class TeamSignInServiceImpl extends ServiceImpl<TeamSignInMapper, TeamSignIn> implements TeamSignInService {

    @Resource
    private TeamCourseMapper teamCourseMapper;
    @Resource
    private OrderFeignClient orderFeignClient;
    @Resource
    private ProductFeignClient productFeignClient;
    @Resource
    private VenueFeignClient venueFeignClient;
    @Resource
    private UserFeignClient userFeignClient;
    @Override
    public void signIn(Integer teamCourseId, Integer userId) {
        TeamCourse teamCourse = teamCourseMapper.selectById(teamCourseId);
        Result<TeamProductDto> productDtoResult = productFeignClient.findTeamProductById(teamCourse.getTeamProductId());
        if (!productDtoResult.getFlag()) throw new TeamSignInException(productDtoResult.getCode(),productDtoResult.getMsg());
        if (BeanUtil.isEmpty(productDtoResult.getData())) throw new TeamSignInException(6010,"该团课产品不存在");
        TeamProductDto productDto = productDtoResult.getData();
        String signIdTime = DateUtil.formatDateTime(new Date());
        TeamSignIn teamSignIn = new TeamSignIn();
        teamSignIn.setUserId(userId);
        teamSignIn.setTeamProductId(productDto.getTeamProductId());
        teamSignIn.setTeamCourseId(teamCourseId);
        teamSignIn.setSignInTime(signIdTime);
        teamSignIn.setVenueId(productDto.getVenueId());
        save(teamSignIn);
    }

    @Override
    public PageInfo<TeamSignInDto> byPage(Integer pageNum, Integer pageSize, Integer venueId) {
        LambdaQueryWrapper<TeamSignIn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeamSignIn::getVenueId,venueId);
        Page<TeamSignIn> page = new Page<>(pageNum, pageSize);
        page(page,wrapper);
        ArrayList<TeamSignInDto> list = new ArrayList<>();
        List<TeamSignIn> records = page.getRecords();
        for (TeamSignIn record : records) {
            TeamSignInDto dto = BeanUtil.toBean(record, TeamSignInDto.class);
            //通过teamSignId中的userId将userInfo查出来
            Result<UserInfoDto> userResult = userFeignClient.findUserById(record.getUserId());
            if (!userResult.getFlag()) throw new TeamSignInException(userResult.getCode(),userResult.getMsg());
            UserInfoDto userInfoDto = userResult.getData();
            dto.setUserName(userInfoDto.getUserName());
            //通过teamSignId中的teamProductId将teamProduct查出来
            Result<TeamProductDto> productResult = productFeignClient.findTeamProductById(record.getTeamProductId());
            if (!productResult.getFlag()) throw new TeamSignInException(productResult.getCode(),productResult.getMsg());
            TeamProductDto productDto = productResult.getData();
            dto.setTeamProductName(productDto.getTeamProductName());
            //通过productDto中的venueId将venueName查出来
            Result<VenueLoginDto> venueLoginDtoResult = venueFeignClient.byId(productDto.getVenueId());
            if (!venueLoginDtoResult.getFlag()) throw new
                    TeamSignInException(venueLoginDtoResult.getCode(),venueLoginDtoResult.getMsg());
            VenueLoginDto venueLoginDto = venueLoginDtoResult.getData();
            dto.setVenueName(venueLoginDto.getVenueName());
            TeamCourse teamCourse = teamCourseMapper.selectById(record.getTeamCourseId());
            dto.setStartTime(teamCourse.getStartTime());
            dto.setEndTime(teamCourse.getEndTime());
            list.add(dto);
        }
        PageInfo<TeamSignInDto> pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public TeamSignInDto byId(Integer teamSignInId) {
        TeamSignIn teamSignIn = getById(teamSignInId);
        TeamSignInDto dto = BeanUtil.toBean(teamSignIn, TeamSignInDto.class);
        Result<UserInfoDto> userResult = userFeignClient.findUserById(teamSignIn.getUserId());
        if (!userResult.getFlag()) throw new TeamSignInException(userResult.getCode(),userResult.getMsg());
        UserInfoDto userInfoDto = userResult.getData();
        dto.setUserName(userInfoDto.getUserName());
        //通过teamSignId中的teamProductId将teamProduct查出来
        Result<TeamProductDto> productResult = productFeignClient.findTeamProductById(teamSignIn.getTeamProductId());
        if (!productResult.getFlag()) throw new TeamSignInException(productResult.getCode(),productResult.getMsg());
        TeamProductDto productDto = productResult.getData();
        dto.setTeamProductName(productDto.getTeamProductName());
        //通过productDto中的venueId将venueName查出来
        Result<VenueLoginDto> venueLoginDtoResult = venueFeignClient.byId(productDto.getVenueId());
        if (!venueLoginDtoResult.getFlag()) throw new
                TeamSignInException(venueLoginDtoResult.getCode(),venueLoginDtoResult.getMsg());
        VenueLoginDto venueLoginDto = venueLoginDtoResult.getData();
        dto.setVenueName(venueLoginDto.getVenueName());
        TeamCourse teamCourse = teamCourseMapper.selectById(teamSignIn.getTeamCourseId());
        dto.setStartTime(teamCourse.getStartTime());
        dto.setEndTime(teamCourse.getEndTime());
        return dto;
    }
}
